# -*- coding: utf-8 -*-
from datetime import datetime

from meiriyitu import db
from flask_login import UserMixin




# gob = db.Table('gob',
#             db.Column('t_id', db.INTEGER, db.ForeignKey('content.t_id')),
#             db.Column('good', db.INTEGER, db.ForeignKey('user.id')),
#             db.Column('bad', db.INTEGER, db.ForeignKey('user.id'))
#                )

class User(UserMixin, db.Model):
    userid = db.Column(db.CHAR(64), primary_key=True)
    nickname = db.Column(db.CHAR(64))
    first_login_time = db.Column(db.DateTime, default=datetime.utcnow)
    last_login_time = db.Column(db.DateTime)
    total_good = db.Column(db.INTEGER)
    total_bad = db.Column(db.INTEGER)
    openid = db.Column(db.CHAR(64),  unique=True, index=True)
    province = db.Column(db.CHAR(64))
    session_key = db.Column(db.CHAR(64))
    login_times = db.Column(db.INT, default=1)
    content = db.relationship('Content', backref='author', lazy='dynamic')



    def __repr__(self):
        return 'id={}, nickname={}, last_login_time={}, total_good={}, total_bad={}'.format(
            self.id, self.nickname, self.last_login_time, self.total_good, self.total_bad
        )



class Content(db.Model):
    t_id = db.Column(db.INTEGER, primary_key=True)
    title = db.Column(db.CHAR(124))
    body = db.Column(db.Text)
    create_time = db.Column(db.DateTime, default=datetime.utcnow)
    userid = db.Column(db.CHAR(64), db.ForeignKey('user.userid'))
    content_name = db.Column(db.CHAR(64), unique=True)
    watch = db.Column(db.INT, default=1)
    url = db.relationship('Content_image_urls', backref='con', lazy='dynamic')
    # good = db.Column(db.INTEGER, db.ForeignKey('user.id'))
    # bad = db.Column(db.INTEGER, db.ForeignKey('user.id'))

    # gob = db.relationship(
    #    'Content', secondary=gob,
    #       primaryjoin=(gob.c.good == id),
    #       secondaryjoin=(gob.c.bad == id),
    #       backref=db.backref('gob', lazy='dynamic'), lazy='dynamic'
    #       )

    def __repr__(self):
        return 't_id={}, content_name={}, body={}, create_time={}, openid={}'.format(
            self.t_id, self.content_name, self.body, self.create_time, self.userid, self.url
        )

class Joke_content(db.Model):
    t_id = db.Column(db.INTEGER, primary_key=True)
    body = db.Column(db.Text)
    create_time = db.Column(db.DateTime, default=datetime.utcnow)
    body_header = db.Column(db.String(64), unique=True)

    def __repr__(self):
        return 't_id={}, body={}, create_time={}, body_header={}'.format(
            self.t_id, self.body, self.create_time, self.body_header
        )


class Content_image_urls(db.Model):
    id = db.Column(db.INTEGER, primary_key=True)
    content_name = db.Column(db.CHAR(64), db.ForeignKey('content.content_name'))
    img_url = db.Column(db.CHAR(128))
    userid = db.Column(db.CHAR(64))
    create_time = db.Column(db.DateTime, default=datetime.utcnow())

    def __repr__(self):
        return 'id={}, content_name={}, img_url={}, userid={}, create_time={}'.format(
            self.id, self.content_name, self.img_url, self.userid, self.create_time
        )


class Calendar_Memo(db.Model):
    id = db.Column(db.INTEGER, primary_key=True)
    userid = db.Column(db.CHAR(64))
    create_time = db.Column(db.DateTime, default=datetime.utcnow())
    memo = db.Column(db.CHAR(128))
    start_datetime = db.Column(db.DateTime)
    finish_datetime = db.Column(db.DateTime)
    isfinish = db.Column(db.Boolean, default=0)
    level = db.Column(db.INTEGER)

    def __repr__(self):
        return 'userid={}, memo={}, start_datetime={}, finish_datetime={}, isfinish={}'.format(
            self.userid, self.memo, self.start_datetime, self.finish_datetime, self.isfinish
        )

    def to_json(self):
        dict = self.__dict__
        if "_sa_instance_state" in dict:
            del dict["_sa_instance_state"]


        return dict